package com.xiaolong.log.util;

import cn.hutool.core.util.StrUtil;
import com.xiaolong.common.utils.SpringUtil;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Objects;

public class InstanceUtil {

    public static String instanceIp() {
        return getLocalIp();
    }

    public static String instanceName() {
        return StrUtil.isBlank(SpringUtil.getString("spring.application.name")) ? "未知服务名称" :
                SpringUtil.getString("spring.application.name");
    }

    public static int instancePort() {
        return Objects.isNull(SpringUtil.getInt("server.port")) ? 0 : SpringUtil.getInt("server.port");
    }

    private static String getLocalIp() {
        try {
            InetAddress localHost = InetAddress.getLocalHost();
            return localHost.getHostAddress();
        } catch (UnknownHostException e) {
            return "0.0.0.0";
        }
    }
}
